from django.shortcuts import render
from django.views import View
from goods.models import GoodsChannel, GoodsCategory
from .models import ContentCategory, Content
from meiduo_mall.utils.meiduo_category import get_categories


# 定义视图,转到首页:index.html
class IndexViews(View):
    def get(self, request):
        # 1查询频道分类信息
        categories = get_categories()
        # 2查询广告信息
        contents = ContentCategory.objects.all()
        contents_dict = {}
        # 遍历广告位
        for content in contents:
            contents_dict[content.key] = content.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': categories,
            'contents': contents_dict
        }

        return render(request, 'index.html', context)
